/*
 * Copyright 2011 J. Auer, J. Andert, F.-J. Jendrusch, U. Celebi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.fuberlin.replication.storage.android.sqlite;

import java.util.Iterator;

import android.database.Cursor;

/**
 * Android-specific abstract SQLite iterator implementation.
 * 
 * @param <T> the transfer object type
 */
public abstract class SQLiteIterator<T> implements Iterator<T> {
	private Cursor cursor;

	/**
	 * Constructor. Moves the cursor to the first row.
	 * 
	 * @param cursor the cursor to iterate over.
	 */
	public SQLiteIterator(Cursor cursor) {
		cursor.moveToFirst();

		this.cursor = cursor;
	}

	/**
	 * Returns the cursor to iterate over.
	 * 
	 * @return the cursor to iterate over.
	 */
	protected Cursor getCursor() {
		return cursor;
	}

	@Override
	public boolean hasNext() {
		return !cursor.isAfterLast();
	}

	/**
	 * Unsupported operation.
	 * 
	 * @throws UnsupportedOperationException unsupported operation.
	 */
	@Override
	public void remove() {
		throw new UnsupportedOperationException();
	}

	/**
	 * @see Cursor#close()
	 */
	@Override
	protected void finalize() throws Throwable {
		try {
			cursor.close();
		} finally {
			super.finalize();
		}
	}
}